package csxXiti.xiti4;

import java.util.*;

/**
 * @author WuYongheng
 * @date 2021/11/23
 * @description
 */
    public class xiti4_3 {
        public List<List<String>> groupAnagrams(String[] strs) {
            if (strs.length == 0)
                return new ArrayList();

            Map<String, List> ans = new HashMap<String, List>();
            for (String s : strs) {
                char[] ca = s.toCharArray();
                Arrays.sort(ca);
                String key = String.valueOf(ca);
                if (!ans.containsKey(key))
                    ans.put(key, new ArrayList());
                ans.get(key).add(s);
            }
            return new ArrayList(ans.values());
        }
        public static void main(String[] args) {
            xiti4_3 pro = new xiti4_3();
            String[] strs = { "eat", "tea", "tan", "ate", "nat", "bat" };
            List<List<String>> res = pro.groupAnagrams(strs);
            for (int i = 0; i < res.size(); i++) {
                for (int j = 0; j < res.get(i).size(); j++) {
                    System.out.print(res.get(i).get(j) + " ");
                }
                System.out.println();
            }
        }
    }

